package com.gitee.free.coding.core.utils;

import com.alibaba.fastjson2.JSON;
import com.gitee.free.coding.core.exceptions.FreeRuntimeException;
import lombok.extern.slf4j.Slf4j;

/**
 * 返回值校验器
 */
@Slf4j
public class ReturnVerifier {

    /**
     * 校验 insert SQL 的返回值
     *
     * @param result 返回值
     * @param object insert SQL 的参数
     * @param <T>    泛型
     */
    public static <T> void verifyInsertOne(int result, T object) {
        if (result != 1) {
            log.info("[新增]执行SQL的返回值：result = {}", result);
            log.info("[新增]执行SQL入参：object = {}", JSON.toJSON(object));
            throw new FreeRuntimeException(-1, "数据新增失败[" + result + "]");
        }
    }

    /**
     * 校验 update SQL 的返回值
     *
     * @param result 返回值
     * @param object update SQL 的参数
     * @param <T>    泛型
     */
    public static <T> void verifyUpdateOne(int result, T object) {
        if (result != 1) {
            log.info("[修改]执行SQL的返回值：result = {}", result);
            log.info("[修改]执行SQL入参：object = {}", JSON.toJSONString(object));
            throw new FreeRuntimeException(-1, "数据修改失败[" + result + "]");
        }
    }

    /**
     * 校验 delete SQL 的返回值
     *
     * @param result 返回值
     * @param object delete SQL 的参数
     * @param <T>    泛型
     */
    public static <T> void verifyDeleteOne(int result, T object) {
        if (result != 1) {
            log.info("[删除]执行SQL的返回值：result = {}", result);
            log.info("[删除]执行SQL入参：object = {}", JSON.toJSONString(object));
            throw new FreeRuntimeException(-1, "数据删除失败[" + result + "]");
        }
    }

}
